package cn.yunhe.controller;

import cn.yunhe.entity.*;
import cn.yunhe.service.QuestionService;
import cn.yunhe.util.ListToQuestionUtil;
import cn.yunhe.util.MultipartFileToFile;
import cn.yunhe.util.ReadExcel;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import org.springframework.web.servlet.view.json.MappingJackson2JsonView;


import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class QuestionController {

    @Autowired
    QuestionService questionService;
    /**
     * restful跳转
     */
    @RequestMapping(value = "/view/question/{ftlUrl}", method = RequestMethod.GET)
    public String toQtlist(@PathVariable("ftlUrl") String ftlUrl) {
        return ftlUrl;
    }

    @RequestMapping(value = "/redirect/{ftlUrl}", method = RequestMethod.GET)
    public String redirect(@PathVariable("ftlUrl") String ftlUrl) {
        return "redirect:/view/"+ftlUrl+".html";
    }


    /**
     * 批量上传
     *
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping("/fileUpload")
    public ModelAndView fileUpload(@RequestParam("fileName") MultipartFile file, @RequestParam("fieldId") Integer fieldId , HttpSession session) throws IOException {
        ModelAndView mv = new ModelAndView();

        if (file.isEmpty()) {
            mv.addObject("flag", "没成功");
            mv.setView(new MappingJackson2JsonView());

            return mv;
        }
        List<List<String>> lists = ReadExcel.readExcel(MultipartFileToFile.translate(file));
        User user=(User) session.getAttribute("Curr_User");
        for (List<String> list : lists) {
            questionService.addQuestion(ListToQuestionUtil.getQuestion(list, fieldId,user.getUserName()));
        }
        mv.addObject("lists", lists);
        mv.setView(new MappingJackson2JsonView());
        return mv;
    }
    /**
     *
     *
     */
    @RequestMapping("/addQuestion")
    public ModelAndView addQuestion(HttpSession session,String name, Integer fieldId, Integer questionTypeId, String A, String B, String C, String D, String pointStr, String answer, String analysis){
        ModelAndView mv = new ModelAndView();
        User user=(User) session.getAttribute("Curr_User");
        String msg = "添加成功";

        Question question = new Question();
        Field field = new Field();
        field.setFieldId(fieldId);
        QuestionType questionType = new QuestionType(questionTypeId);
        question.setName(name);
        question.setField(field);
        question.setQuestionType(questionType);
        question.setAnswer(answer);
        question.setAnalysis(analysis);
        question.setA(A);
        question.setB(B);
        question.setC(C);
        question.setD(D);
        question.setLastModify(new Date());
        question.setCreator(user.getUserName());
        question.setCreateTime(new Date());
        if (answer != null && answer != "" && name != null && name != "") {
            questionService.addQuestion(question);
            if (pointStr!=null &&pointStr!=""){
                String[] pointArr = pointStr.split(",");
                List<Integer> pointList = new ArrayList<Integer>();
                for (String p : pointArr
                        ) {
                    pointList.add(Integer.parseInt(p));
                }
                for (Integer pointId : pointList
                        ) {
                    questionService.addQuestionPoint(question.getId(), pointId);
                }
            }
        } else {
            msg = "请检查你的字段是不是空的";
        }
        mv.addObject("msg", msg);
        mv.addObject("id",question.getId());
        mv.setView(new MappingJackson2JsonView());
        return mv;
    }
    /**
     * 题库搜索
     *
     * @return
     */
    @RequestMapping("/queryQuestion")
    public ModelAndView queryQuestion(Integer currPage, String questionName, Integer questionTypeId, Integer fieldId, Integer pointId, String creator) {
        ModelAndView mv = new ModelAndView();
        Question question = new Question();

        if (fieldId != null) {
            Field field = new Field();
            field.setFieldId(fieldId);
            question.setField(field);
        }

        if (questionTypeId != null) {
            QuestionType questionType = new QuestionType();
            questionType.setId(questionTypeId);
            question.setQuestionType(questionType);
        }

        if (pointId != null) {
            List<Point> list = new ArrayList<Point>();
            Point point1 = new Point();
            point1.setPointId(pointId);
            list.add(point1);
            question.setPointList(list);
        }

        if (questionName != null) {
            question.setName(questionName);
        }

        if (creator != null) {
            question.setCreator(creator);
        }

        Page page = questionService.queryQuestion(question, currPage);
        mv.addObject("page", page);
        mv.addObject("totalPages", page.getPages());
        mv.setView(new MappingJackson2JsonView());
        return mv;
    }

    /**
     * 查询详情
     */
    @RequestMapping("/questionDetail")
    public ModelAndView questionDetail(Integer id, String href) {
        ModelAndView mv = new ModelAndView();
        Question question = questionService.queryQuestionById(id);
        mv.addObject("question", question);
        if (href != null && href != "") {
            mv.setViewName(href);
        } else {
            mv.setViewName("question-detail");
        }
        return mv;
    }

    /**
     * 修改试题
     */
    @RequestMapping("/questionModify")
    public ModelAndView questionModify(Integer id, String name, Integer fieldId, Integer questionTypeId, String A, String B, String C, String D, String pointStr, String answer, String analysis) {
        ModelAndView mv = new ModelAndView();
        String msg = "修改成功";

        Question question = new Question();
        Field field = new Field();
        field.setFieldId(fieldId);
        QuestionType questionType = new QuestionType(questionTypeId);
        question.setId(id);
        question.setName(name);
        question.setField(field);
        question.setQuestionType(questionType);
        question.setAnswer(answer);
        question.setAnalysis(analysis);
        question.setA(A);
        question.setB(B);
        question.setC(C);
        question.setD(D);
        question.setLastModify(new Date());

        if (answer != null && answer != "" && name != null && name != "") {
            questionService.upd(question);
            if (pointStr!=null &&pointStr!=""){
                String[] pointArr = pointStr.split(",");
                List<Integer> pointList = new ArrayList<Integer>();
                for (String p : pointArr
                        ) {
                    pointList.add(Integer.parseInt(p));
                }
                questionService.delQuestionPoint(question.getId());
                for (Integer pointId : pointList
                        ) {
                    questionService.addQuestionPoint(question.getId(), pointId);
                }
            }else{
                questionService.delQuestionPoint(question.getId());
            }

        } else {
            msg = "请检查你的字段是不是空的";
        }


        mv.addObject("msg", msg);
        mv.setView(new MappingJackson2JsonView());
        return mv;
    }

     @RequestMapping("/getQuestionCount")
     @ResponseBody
    public  int getQuestionCount(){
        return questionService.getQuestionCount();
    }

    /**
     * 获取各科目试题数目占比
     * @return FieldList
     * 郭瀚钰
     */
    @RequestMapping("/getQuestionCountGroupByField")
    @ResponseBody
    public List getQuestionCountGroupByField(){
        List<Field> questionCountByFieldList = questionService.getQuestionCountGroupByFieldId();
        return questionCountByFieldList;
    }

}
